package com.lottery.im.module.service.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.interceptor.OnSmartRefreshListener;
import com.lottery.im.api.observer.BaseListObserver;
import com.lottery.im.api.result.ApiListResult;
import com.lottery.im.api.result.entity.msg_bulletin.Bulletins;
import com.lottery.im.api.result.entity.msg_bulletin.Messages;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.MessageAndBullentinView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MessageAndBulletinPresenter extends BaseFragmentPresenter<MessageAndBullentinView> {

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();

  public MessageAndBulletinPresenter(Activity activity,
      MessageAndBullentinView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void getMessages(int startRow,int pageSize,OnSmartRefreshListener listener){
    ApiClient.getApiRetrofitInstance()
        .querySystemMessageList(userId,token,String.valueOf(startRow),String.valueOf(pageSize))
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseListObserver<Messages>() {
          @Override public void onStart() {
            showLoadingProgress("加载中");
          }

          @Override public void onSuccess(List<Messages> t) {
            closeLoadingProgress();
            if(listener==null){
              iView.loadMessageSuccess(t);
            }else{
              listener.onRefreshComplete(t,true);
            }
          }

          @Override public void onFail(String code, String msg) {
            closeLoadingProgress();
            if(listener!=null){
              listener.onRefreshComplete(null,false);
            }
          }

          @Override public void onException(String msg) {
            closeLoadingProgress();
            if(listener!=null){
              listener.onRefreshComplete(null,false);
            }
          }

          @Override public void onFinish() {
            closeLoadingProgress();
          }
        });
  }

  public void getBulltins(int startRow,int pageSize,OnSmartRefreshListener listener){
    ApiClient.getApiRetrofitInstance()
        .getBulletin(userId,token,String.valueOf(startRow),String.valueOf(pageSize))
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.<ApiListResult<Bulletins>>bindToLifecycle())
        .subscribe(new BaseListObserver<Bulletins>() {
          @Override public void onStart() {

          }

          @Override public void onSuccess(List<Bulletins> t) {
            if(listener==null){
              iView.loadBulletinSuccess(t);
            }else{
              listener.onRefreshComplete(t,true);
            }
          }

          @Override public void onFail(String code, String msg) {
            if(listener!=null){
              listener.onRefreshComplete(null,false);
            }
          }

          @Override public void onException(String msg) {
            if(listener!=null){
              listener.onRefreshComplete(null,false);
            }
          }

          @Override public void onFinish() {

          }
        });
  }
}
